﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>下发计划</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link href="../../static/css/font.css" rel="stylesheet" />
    <link href="../../static/css/weadmin.css" rel="stylesheet" />
    <link href="../../lib/layui/css/layui.css" rel="stylesheet" />
    <script src="../../../lib/layui/layui.js"></script>

</head>
<body>
    <div class="layui-fluid" id="infos">
        <table id="dataList" lay-filter="dataList"></table>
    </div>
    <div class="layui-fluid fluid-position">
        <div class="layui-form">
            <div class="layui-card">
                <!--<div class="layui-card-header">
                    <div class="layui-row layui-col-space10 layui-btn-container">

                        <button type="button" class="layui-btn layui-btn-primary" id="find" name="查询"><i class="layui-icon">&#xe615;</i>查询</button>
                        <button type="button" class="layui-btn layui-btn-primary" id="reset" name="清空"><i class="layui-icon">&#xe669;</i>清空</button>
                        <button type="button" class="layui-btn layui-btn-normal" id="add" name="添加保存"><i class="layui-icon">&#xe608;</i> 添加保存</button>
                        <button type="button" class="layui-btn layui-btn-normal" id="edit" lay-filter="edit" name="下发计划" lay-submit><i class="layui-icon">&#xe642;</i>下发计划</button>
                        <button type="button" class="layui-btn layui-btn-danger" id="del" name="删除"><i class="layui-icon">&#xe640;</i>删除</button>

                        <button type="button" class="layui-btn" name="导入"><i class="layui-icon">&#xe638;</i>导入</button>
                        <button type="button" class="layui-btn" name="导出"><i class="layui-icon">&#xe632;</i>导出</button>
                        <button type="button" class="layui-btn" name="打印"><i class="layui-icon">&#xe63c;</i>打印</button>
                    </div>
                </div>-->
                <div class="layui-card-body">
                    <div class="layui-collapse" lay-filter="infos_form">
                        <div class="layui-colla-item">
                            <h2 class="layui-colla-title">下发计划</h2>
                            <div class="layui-colla-content layui-show">
                                <div class="layui-form layui-form-pane">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">计划名称</label>
                                        <div class="layui-input-block">
                                            <input type="text" placeholder="计划名称" class="layui-input" maxlength="50" id="TPName" name="TPName" disabled />
                                        </div>
                                    </div>
                                    <div class="layui-form-item">

                                    </div>
                                    <div class="layui-form-item">
                                        <div class="layui-form-label">下发部门</div>
                                        <div class="layui-input-block">
                                            <input type="text" autocomplete="off" readonly="" id="selectDatas" name="selectDatas" placeholder="请选择" class="layui-input" />
                                            <div class="eleTree" lay-filter="data5"></div>
                                            <button type="button" class="layui-btn layui-btn-normal" id="edit" lay-filter="edit" name="下发计划" lay-submit><i class="layui-icon">&#xe642;</i>下发计划</button>

                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-card-header layui-btn-container">

                </div>
            </div>
        </div>
    </div>
    <script>
        var ModleNameTip = "下发计划";  //模块名称  用于提示部门信息的内容
        var contollerName = "TrainPlanSendStep";
        var CurrentGetTPID = "";//传过来的计划id
        var CurrentGetTPName = "";//传过来的计划名称
        var SendState = true;
        var SendID = 0;//下发阶段id

        var load = 1;
        var CPID = 0;//所属集团计划id  带过来的 或者直接选过来的
        var prePlanId = 0;//选择的上级培训计划id
        var selectIDs = [];//选中的id列表 [可用批量删除  审核 取消 ]
        var selectCurrentID = 0;// 当前选中的 id  [用于修改]
        var currentDeptLevelId = 0;//当前登录者 部门级别  0：是段
        var currentInfoDeptLevelId = 0;//当前选中信息的  部门级别  [用于判断 是否能够对 该条信息进行修改:下级不能对上级的计划信息进行修改]
        var cuurentDeptId = 0;//当前部门id

        var selectCurrentIsCheckStatus = 0;// 当前选中的行的 IsChck 状态  [用于删除/审核/取消审核/发布/取消发布时判断]
        var selectCurrentIsReleaseStatus = 0;// 当前选中的行的 IsRelease 状态  [用于删除/审核/取消审核/发布/取消发布时判断]

        var currentInfoDeptId = 0;//当前选中信息的 部门id
        var selectobjs = [];//
        //加载 [赋值]获取数据  (部门相关数据)
        var dataSelect = [];
        var temp_deptId_List = [];//列表中选择信息时   使用字段中的内容=>为其赋值,可以使用此字段保存
        var dataCurent = [];
        var th = 0;
        var tw = 0;
        layui.use(['form', 'laydate', 'table', 'jquery', 'element', 'layer', 'tree', 'util'], function () {
            var laydate = layui.laydate;
            var $ = layui.jquery;
            th = $(document).height() * 0.5;
            tw = $(document).width();
            var table = layui.table;
            var form = layui.form;
            var element = layui.element;
            var layer = layui.layer;
            var eleTree = layui.tree;
            CurrentGetTPID = GetQueryString("TPID");
            CurrentGetTPName = GetQueryString("TPName");
            $(".layui-card-header").find("button").each(function () { if (PowerList.indexOf($(this).attr("name")) <= -1) { $(this).attr("style", "display:none;") } });
            form.on('switch(switch)', function (data) { });
            laydate.render({ elem: '#TPReleaseStart', trigger: 'click', type: 'datetime', format: 'yyyy-MM-dd HH:mm' });
            laydate.render({ elem: '#TPReleaseEnd', trigger: 'click', type: 'datetime', format: 'yyyy-MM-dd HH:mm' });
            function PageInit() {
                var obj = $("div[lay-id='dataList']");
                var child = $(".layui-table-body");
                var off = obj.offset().top;
                var c = th - off - 100;
                child.attr("style", "height:" + c + "px");
            }
            element.on('collapse(infos_form)', function (data) {
                PageInit();
            });
            function formRenderSelect() {
                form.render('select');
            }
            //获取当前 部门级别id,部门id
            $.ajax({
                url: ApiUrl + "Dep/DeptLevel",//SelTree
                type: 'post',
                headers: {
                    "userid": JSON.parse(window.localStorage.authInfo).UserId,
                    "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                },
                success: function (data) {
                    if (data.StatusCode == 200) {
                        currentDeptLevelId = data.Data.currentDeptLevelId;
                        cuurentDeptId = data.Data.cuurentDeptId;
                        GetDept(cuurentDeptId);
                    }
                    else if (data.StatusCode == 403) {
                        layer.alert(data.Info, function () {
                            top.location.href = "http://" + top.location.host + "/login.html";
                        })
                    }
                    else {
                        layer.msg(data.Info);
                    }
                }
            });
            //获取下级部门列表树
            function GetDept(deptid) {
                var r = 0;
                $.ajax({
                    url: ApiUrl + "Dep/SelNextTree",//SelTree
                    type: 'post',
                    async: false,
                    headers: {
                        "userid": JSON.parse(window.localStorage.authInfo).UserId,
                        "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                    },
                    data: { DepID: deptid },
                    success: function (data) {
                        if (data.StatusCode == 200) {
                            dataSelect = data.Data;
                            loadTree();
                            r = 1;
                        }
                        else if (data.StatusCode == 403) {
                            layer.alert(data.Info, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                        else {
                            layer.msg(data.Info);
                        }
                    }
                });
                return r;
            }
            var tempList = [];
            function SetTextFromTree(v) {
                for (var i = 0; i < v.length; i++) {
                    var c = v[i];
                    tempList.push({ id: c.id, title: c.title });
                    temp_deptId_List.push(c.id);
                    var a = $("#selectDatas").val();
                    if (a != "") { a += ","; }
                    $("#selectDatas").val(a + c.title);
                    if (c.children != undefined) {
                        if (c.children.length > 0) {
                            SetTextFromTree(c.children);
                        }
                    }
                }
            }
            function setTree()//选择信息列表中 信息时 调用该方法  并为temp_deptId_List赋值
            {
                eleTree.setChecked('eleTreeid', temp_deptId_List);
            }
            var el_tree;
            $("#selectDatas").on("click", function (e) {
                e.stopPropagation();
                loadTree();
                $(".eleTree").toggle();
            });
            function loadTree() {
                el_tree =
                    eleTree.render({
                        elem: '.eleTree',
                        id: 'eleTreeid',
                        data: dataSelect,
                        defaultExpandAll: false,
                        expandOnClickNode: false,
                        highlightCurrent: true
                        , checkStrictly: true
                        , showCheckbox: true
                        , oncheck: function (obj) {
                            $("#selectDatas").val("");
                            temp_deptId_List = [];
                            tempList = [];
                            SetTextFromTree(el_tree.getChecked());
                        }
                    });
            }
            function showList() {
                var swhere = JSON.stringify({
                    TPID: CurrentGetTPID
                });
                table.render({
                    elem: '#dataList'
                    , url: ApiUrl + contollerName + '/Search'
                    , id: "dataList"
                    , headers: {
                        "userid": JSON.parse(window.localStorage.authInfo).UserId,
                        "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                    }
                    , page: true
                    , cols: [[
                        { type: 'checkbox' },
                        { field: 'AutoID', title: 'AutoID', sort: true, hide: true },
                        { field: 'TPID', title: '集团编号', sort: true, hide: true },
                        { field: 'SendID', title: 'ID', sort: true, hide: true },
                        { field: 'DepID', title: 'DepID', sort: true, hide: true },
                        { field: 'SendUserID', title: '下发者id', sort: true, hide: true },
                        { field: 'CreateUserID', title: '创建者id', sort: true, hide: true },
                        { field: 'DeptLevel', title: 'DeptLevel', sort: true, hide: true },
                        { field: 'TPSDID', title: 'TPSDID', sort: true, hide: true },
                        { field: 'TPName', title: '计划名称'},
                        { field: 'SendStep', title: '下发阶段', width: 100 },
                        {
                            field: 'SendDateTime', title: '下发日期', width: 120, templet: function (d) {
                                return d.SendDateTime.substring(0, 10);
                            },
                        },
                       
                        { field: 'CreateUserName', title: '创建人', width: 150 },
                        {
                            field: 'CreateDateTime', title: '创建日期', width: 120, templet: function (d) {
                                return d.CreateDateTime.substring(0, 10);
                            },
                        },
                    ]]
                    , where: { swhere: swhere }
                    , parseData: function (res) { //res 即为原始返回的数据
                        return {
                            "code": res.StatusCode, //解析接口状态
                            "msg": res.Info, //解析提示文本
                            "count": res.Count, //解析数据长度
                            "data": res.Data //解析数据列表
                        };
                    },
                    response: { statusCode: 200 },//规定成功的状态码，默认：0
                    event: true,
                    page: true,
                    done: function (res, curr, count) {
                        //如果是异步请求数据方式，res即为你接口返回的信息。
                        //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                        if (res.data != null) {
                            var p = $(".layui-laypage-skip").find("input[type=text]").val();
                            var c = $(".layui-laypage-limits select").find("option:selected").text().substring(0, 2);
                            var k = (Number(p) - 1) * Number(c);
                            for (var i = 0; i < res.data.length; i++) {
                                var item = res.data[i];
                                var ix = item.Row - 1 - k; 
                                $(".layui-table-body table tbody tr[data-index=" + ix + "]").attr("style", dataRowColor[item.DeptLevel]);
                            }
                        }

                        if (res.code == 403) {
                            layer.alert(res.msg, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                    }
                });
            }
            showList();
            //监听指定开关
            form.on('switch(sendNext)', function (data) {
                SendState = this.checked;
            });
            $(function () {
                $("del").hide();
                document.getElementById("edit").style.display = "none";
                document.getElementById("edit").style.display = "inline";
                $("#sreach").click(function () {
                    showList();
                })
            })
            PageInit();
            function clearForm() {
                $("textarea").val("");
                $("input").val("");
                $("#TPIsRelease").val("");
                $("#TPName").val(CurrentGetTPName);
            }
            clearForm();
            function GetSendDeptListByStepId(SendID,TPID) {
                var apiUrl = "TrainPlanSendDept/GetSendDeptListByStepId?TPSSID=" + SendID + "&&TPID=" + TPID +"";
                $.ajax({
                    url: ApiUrl + apiUrl,
                    type: 'get',
                    headers: {
                        "userid": JSON.parse(window.localStorage.authInfo).UserId,
                        "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                    },
                    success: function (data) {
                        if (data.StatusCode == 200) {
                            temp_deptId_List = [];
                            for (var i = 0; i < data.Data.length; i++) {
                                temp_deptId_List.push(Number(data.Data[i].TPSDDeptID));
                            }
                            setTree();
                        }
                        else if (data.StatusCode == 403) {
                            layer.alert(data.Info, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                        else {
                            layer.msg(data.Info);
                        }
                    }
                });
            }
            //展示一条信息
            function GetSingleInfo(data) {
                SetselectValue($("#sendNext"), data.SendState, formRenderSelect);
                $("#TPName").val(data.TPName);
                currentInfoDeptLevelId = data.DeptLevel;
                currentInfoDeptId = data.DeptID;
                SendID = data.SendID;
                var r = GetDept(currentInfoDeptId);
                if (r == 1) {
                    GetSendDeptListByStepId(data.SendID, data.TPID);
                }
            }

            ///按钮操作 开始
            //监听行单击事件（单击事件为：rowDouble）
            table.on('row(dataList)', function (obj) {
                var checked = false;
                var objcheck = $(obj.tr.selector).find("td").eq(0).find(".layui-table-cell").eq(0).find("div").eq(0);
                if (selectIDs.indexOf(obj.data.SendID) > -1) {
                    objcheck.removeClass("layui-form-checked");
                }
                else {
                    objcheck.addClass("layui-form-checked"); checked = true;
                }
                if (checked) {
                    if (obj.data.SendID != undefined) {
                        selectCurrentID = obj.data.SendID;
                        selectIDs.push(obj.data.SendID);
                        selectobjs.push(obj.data);
                        GetSingleInfo(obj.data);
                    }
                    else {
                        selectIDs = [];
                        selectCurrentID = 0;
                        selectobjs = [];
                        var dataList = table.checkStatus("dataList").data;
                        for (var i = 0; i < dataList.length; i++) {
                            var item = dataList[i];
                            selectobjs.push(item);
                            selectIDs.push(item.SendID);
                            if (i == dataList.length - 1) {
                                GetSingleInfo(item);
                                selectCurrentID = item.SendID;
                            }
                        }
                    }
                }
                else {
                    if (obj.data.SendID != undefined) {
                        selectIDs.splice(selectIDs.indexOf(obj.data.SendID), 1);
                        for (var i = 0; i < selectobjs.length; i++) {
                            if (selectobjs[i].SendID == obj.data.SendID) {
                                if (i == selectobjs.length - 1) {
                                    selectCurrentID = 0;
                                    selectCurrentName = "";
                                    if (i > 0) {
                                        if (selectobjs[i - 1].SendID != undefined) {
                                            selectCurrentID = selectobjs[i - 1].SendID;
                                            GetSingleInfo(selectobjs[i - 1]);
                                        }
                                    }
                                    else {
                                        clearForm(); selectCurrentID = 0;
                                    }
                                }
                                selectobjs.splice(i, 1);
                                break;
                            }
                        }
                        if (selectIDs <= 0) {
                            temp_deptId_List = [];
                            var r = GetDept(cuurentDeptId);
                            if (r == 1) {
                                setTree();
                            }
                        }
                    }
                    else {
                        selectIDs = [];
                        selectCurrentID = 0;
                        selectobjs = [];
                        clearForm();
                        temp_deptId_List = [];
                        var r = GetDept(cuurentDeptId);
                        if (r == 1) {
                            setTree();
                        }
                    }
                }
            });
            table.on('checkbox(dataList)', function (obj) {
                if (obj.data.AutoID == undefined) {
                    selectIDs = [];
                    selectCurrentID = 0;
                    selectobjs = [];
                    if (obj.checked) {
                        var dataList = table.checkStatus("dataList").data;
                        for (var i = 0; i < dataList.length; i++) {
                            var item = dataList[i];
                            selectobjs.push(item);
                            selectIDs.push(item.SendID);
                            if (i == dataList.length - 1) {
                                GetSingleInfo(item);
                                selectCurrentID = item.SendID;
                            }
                        }
                    }
                    else {
                        selectIDs = [];
                        selectCurrentID = 0;
                        selectobjs = [];
                        clearForm();
                        temp_deptId_List = [];
                        var r = GetDept(cuurentDeptId);
                        if (r == 1) {
                            setTree();
                        }
                    }
                }
            });

            $("#find").click(function () {
                showList();
            });

            $("#reset").click(function () {
                reset();
            });
            function reset() {
                clearForm();
                temp_deptId_List = [];
                selectCurrentID = 0;
                GetDept(cuurentDeptId);
                setTree();}
            //校验数据
            function CheckData() {
                if (CurrentGetTPID <= 0) {
                    layer.msg("请选择上级计划！");
                    return false;
                }
                if (temp_deptId_List.length<=0) {
                    layer.msg("请选择下发部门！");
                    return false;
                }
            
                return true;
            }
            //获得数据
            function GetFormData() {
                var TPID = CurrentGetTPID;//培训计划id
                return model = {
                    TPID: TPID,
                    SendNextDeptIds: temp_deptId_List,
                    SendID: SendID
                }
            }
            //判断当前 能否操作该信息
            function GetOperate(operRatetype, pcurrentInfoDeptLevelId) {
                //operRatetype:操作类型     pcurrentInfoDeptLevelId:当前需要操作的信息的  部门级别
                //operRatetype[1:修改，2：删除,3:审核,4:取消审核,5:发布 6:取消发布]
                var result = true;
                if (currentDeptLevelId > pcurrentInfoDeptLevelId) {
                    switch (operRatetype) {
                        case 1:
                            layer.msg("您没有权限，不能修改上级" + ModleNameTip + "");
                            result = false;
                            break;
                        case 2:
                            layer.msg("您没有权限，不能删除上级" + ModleNameTip + "");
                            result = false;
                            break;
                        case 3:
                            layer.msg("您没有审核权限，不能对该条信息进行审核");
                            result = false;
                            break;
                        case 4:
                            layer.msg("您没有取消审核权限，不能对该条信息进行取消审核");
                            result = false;
                            break;
                        case 5:
                            layer.msg("您没有发布信息权限，不能对该条信息进行发布");
                            result = false;
                            break;
                        case 6:
                            layer.msg("您没有取消发布权限，不能对该条信息进行取消发布");
                            result = false;
                            break;
                    }
                }
                return result;
            }

            
            function add() {
                if (!CheckData()) { return; }
                $.ajax({
                    url: ApiUrl + contollerName + "/Add",
                    type: 'post',
                    dataType: 'json',
                    cache: false,
                    data: GetFormData(),
                    headers: {
                        "userid": JSON.parse(window.localStorage.authInfo).UserId,
                        "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                    },
                    success: function (data) {
                        if (data.StatusCode == 200 && data.Count != 0) {
                            layer.alert("添加保存成功", { icon: 6 }, function (index) {
                                layer.close(index); reset();
                                location.reload();
                            })
                        }
                        else if (data.StatusCode == 403) {
                            top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                        else {
                            layer.msg(data.Data);
                        }
                    }
                });
            }


            //编辑保存
            form.on('submit(edit)', function (formdata) {
                if (selectCurrentID <= 0) {
                    add();
                }
                else {
                    edit();
                }
            });
            function edit() {
                if (!GetOperate(1, currentInfoDeptLevelId)) { return; }
                if (!CheckData()) { return; }
                var model = GetFormData();
                model.TPID = selectCurrentID;
                //编辑保存 selectCurrentID
                $.ajax({
                    url: ApiUrl + contollerName + "/Edit",
                    type: 'post',
                    dataType: 'json',
                    data: model,
                    headers: {
                        "userid": JSON.parse(window.localStorage.authInfo).UserId,
                        "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                    },
                    success: function (data) {
                        if (data.StatusCode == 200 && data.Count != 0) {
                            layer.alert(data.Data, { icon: 6 }, function (index) {
                                layer.close(index); reset();
                                location.reload();
                            })
                        }
                        else if (data.StatusCode == 403) {
                            top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                        else {
                            layer.msg(data.Data);
                        }
                    }
                });

            }

            //删除
            $("#del").click(function () {
                if (selectCurrentID <= 0) {
                    layer.msg("请选择一条信息!"); return;
                }
                if (!GetOperate(2, currentInfoDeptLevelId)) { return; }
                if (selectIDs.length == 0) {
                    layer.msg("请选择要删除的" + ModleNameTip + "！");
                    return;
                }
                //if (selectCurrentIsCheckStatus == 1) {
                //    layer.msg("已审核的" + ModleNameTip + "不能删除！");
                //    return;
                //}
                //if (selectCurrentIsReleaseStatus == 1) {
                //    layer.msg("已发布的" + ModleNameTip + "不能删除！");
                //    return;
                //}
                layer.confirm('确定要删除吗,已下发的将不会执行删除?', function (index) {


                    var delList = '';
                    for (var i = 0; i < selectobjs.length; i++) {
                        delList += selectobjs[i].TPID + ',';
                    }
                    delList = delList.substring(0, delList.length - 1);
                    $.ajax({
                        url: ApiUrl + contollerName + "/DelList",
                        type: 'get',
                        cache: false,
                        headers: {
                            "userid": JSON.parse(window.localStorage.authInfo).UserId,
                            "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                        },
                        data: { TPIDlist: delList },
                        success: function (data) {
                            if (data.StatusCode == 200) {
                                layer.alert(data.Data, { icon: 6 }, function (index) {
                                    layer.close(index);
                                    location.reload();
                                })
                            }
                            else if (data.StatusCode == 403) {
                                layer.alert(data.Info, function () {
                                    top.location.href = "http://" + top.location.host + "/login.html";
                                })
                            }
                            else {
                                layer.msg(data.Info);
                            }
                        }
                    });


                    //删除 selectIDs
                    layer.close(index);
                });
            });
           

            //end
        });
    </script>
</body>
</html>